﻿import { _decorator, Component, Node, Quat, Vec3 } from "cc";
const { ccclass, property } = _decorator;

@ccclass("MapNavAgent")
export default class MapNavAgent extends Component {

    private speed: number = 5.0;
    private roadData: Array<any> = null;

    private nextStep: number = 1; // 
    private vx: number;
    private vy: number;
    private vz: number;
    private walkTime: number = 0.0;
    private passedTime: number = 0.0;
    private isWalking: boolean = false;

    private dstRot: Quat = null;
    private now: Quat = new Quat();

    private endFunc: Function = null;
    
    public InitMapNavAgent(speed: number): void {
        this.speed = speed;
    }

    public NavOnRoad(roadData: Array<any>, endFunc: Function): void { // 角色从路径起点开始导航
        this.roadData = roadData;
        this.endFunc = endFunc;
        this.isWalking = false;
        if (this.roadData == null || this.roadData.length < 2) {
            return;
        }

        this.node.setWorldPosition(this.roadData[0].x, this.roadData[0].y, this.roadData[0].z);
        this.nextStep = 1;
        this.MoveToNext();
    }

    public NavOnRoadWithFirst(roadData: Array<any>, endFunc: Function): void { // 角色从当前点导航，第一个点目标的起点;
        this.roadData = roadData;
        this.endFunc = endFunc;
        this.isWalking = false;
        if (this.roadData == null || this.roadData.length < 1) {
            return;
        }

        this.nextStep = 0;
        this.MoveToNext();
    }

    private MoveToNext(): void { // 从当前点---->this.roadData[this.nextStep]
        if (this.nextStep >= this.roadData.length) { // 走完了;
            this.isWalking = false;
            if(this.endFunc) {
                this.endFunc();
            }
            return;
        }

        var src: Vec3 = this.node.getWorldPosition();
        var dst: Vec3 = cc.v3(this.roadData[this.nextStep].x, this.roadData[this.nextStep].y, this.roadData[this.nextStep].z);

        var dir: Vec3 = cc.v3(0, 0, 0);
        Vec3.subtract(dir, dst, src);
        var len = Vec3.len(dir);
        if (len <= 0) { // 如果两个点距离相等
            this.nextStep ++;
            this.MoveToNext();
            return;
        }

        this.vx = this.speed * dir.x / len;
        this.vy = this.speed * dir.y / len;
        this.vz = this.speed * dir.z / len;

        this.walkTime = len / this.speed;
        this.passedTime = 0;

        this.isWalking = true;

        var old: Quat = this.node.getRotation();
        this.node.lookAt(dst); // 当前得方向 ----> 目标方向;
        this.dstRot = this.node.getRotation();
        this.node.setRotation(old);

    }

    public StopNav(): void {
        this.isWalking = false;
    }

	public update(dt: number): void {
        if (!this.isWalking) {
            return;
        }

        // var dt = Time.deltaTime;
        this.passedTime += dt;
        if (this.passedTime > this.walkTime) { 
            dt -= (this.passedTime - this.walkTime);
        }

        var pos = this.node.getWorldPosition();
        pos.x += (this.vx * dt);
        pos.y += (this.vy * dt);
        pos.z += (this.vz * dt);
        this.node.setWorldPosition(pos);


        // 四元数得球面插值;
        Quat.slerp(this.now, this.node.getRotation(), this.dstRot, 10.0 * dt);
        this.node.setRotation(this.now);
        // end 

        if (this.passedTime >= this.walkTime) {
            this.nextStep ++;
            this.MoveToNext();
        }
	}
}
